
package com.xiaocikuai.smallchunks.service.Recode;

import com.xiaocikuai.smallchunks.entity.Card.CardChunks;
import com.xiaocikuai.smallchunks.entity.Recode.Recode;
import com.xiaocikuai.smallchunks.mapper.Card.CardChunksMapper;
import com.xiaocikuai.smallchunks.mapper.Recode.RecodeMapper;
import com.xiaocikuai.smallchunks.mapper.User.UserMapper;
import com.xiaocikuai.smallchunks.utils.JWTservice.JwtService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;


@Service
public class RecodeService {
    @Resource
    private RecodeMapper recodeMapper;
    @Resource
    private JwtService jwtService;
    @Resource
    private UserMapper userMapper;

    @Resource
    private CardChunksMapper cardChunksMapper;

    //学习操作
//    public int learnRecode(Recode recode, String token) {
//        recode.setUid(jwtService.deconstruction(token).getUid());
//        int score = 0;
//        Recode existingRecode = recodeMapper.getRecode(recode.uid, recode.cid);//获取上一次学习记录
//        recode.setTime(LocalDate.now());
//        if (existingRecode == null) {//新学
//            if (recode.getCorrect() == 1) {
//                score = 2;
//            }
//            recode.setLevel(0);
//            userMapper.addIntegrals(recode.uid,score);//新加积分
//            recodeAdd(recode);
//        } else {
//            if (existingRecode.getTime().isEqual(LocalDate.now())){
//                recodeUpdate(recode,existingRecode);
//            } else {
//                recodeAdd(recode);
//            }
//        }
//        return score;
//    }
//
//
//
//    //更新学习记录
//// 更新学习记录
//    public boolean recodeUpdate(Recode recode, Recode lastRecode) {
//        int recodeCorrect = recode.getCorrect();
//        int lastRecodeCorrect = lastRecode.getCorrect();
//
//        if (recodeCorrect!= lastRecodeCorrect) {
//            switch (recodeCorrect) {
//                case 2:
//                    recode.setLevel(recode.getLevel() - 1);
//                    recode.setReview(LocalDate.now());
//                    break;
//                case 1:
//                    recode.setLevel(recode.getLevel() + 1);
//                    recode.setReview(getReviewTime(LocalDate.now(), recode.getLevel()));
//                    break;
//            }
//        }
//        return recodeMapper.recodeUpdate(recode);
//    }
//
//    //添加学习记录
//    public boolean recodeAdd(Recode recode){
//        if (recode.correct == 1) {
//            if (recode.level >= 7) {
//                recode.level = 9;
//            } else {
//                recode.level += 2;
//            }
//            recode.review = getReviewTime(LocalDate.now(),recode.level);
//        } else if (recode.correct == 2) {
//            if (recode.level > 0) {
//                recode.level -= 1;
//            }
//            recode.review = LocalDate.now();
//        }
//        return recodeMapper.recodeAdd(recode);
//    }

    public int learnRecode(Recode recode, String token) {
        int score = 0;
        recode.setUid(jwtService.deconstruction(token).getUid());
        Recode existingRecode = recodeMapper.getRecode(recode.getUid(), recode.getCid());//获取上一次学习记录
        recode.setTime(LocalDate.now());
        if (existingRecode == null) {//新学
            if (recode.getCorrect() == 1) {
                score = 2;
            }
            recode.setLevel(0);
            userMapper.addIntegrals(recode.getUid(),score);//新加积分
            recodeMapper.recodeAdd(setLevel(recode));
        } else {
            if (existingRecode.getTime().isEqual(LocalDate.now())){
                recode.setLevel(existingRecode.getLevel());
                recodeUpdateToday(recode, existingRecode);
            } else {
                recode.setLevel(existingRecode.getLevel());
                recodeMapper.recodeAdd(setLevel(recode));
            }
        }
        return score;
    }

    public boolean recodeUpdateToday(Recode recode,Recode existingRecode) {
        if (recode.getCorrect() != existingRecode.getCorrect()) {
            switch (recode.getCorrect()) {
                case 2:
                    recode.setLevel(recode.getLevel() - 1);
                    recode.setReview(LocalDate.now());
                    break;
                case 1:
                    recode.setLevel(recode.getLevel() + 1);
                    recode.setReview(getReviewTime(LocalDate.now(), recode.getLevel()));
                    break;
            }
            return recodeMapper.recodeUpdate(recode);
        } else {
            return false;
        }
    }




    public Recode setLevel(Recode recode){
        if (recode.getCorrect() == 1) {
            if (recode.getLevel() >= 7) {
                recode.setLevel(9);
            } else {
                recode.setLevel(recode.getLevel() + 2);
            }
            recode.review = getReviewTime(LocalDate.now(),recode.getLevel());
        } else if (recode.getCorrect() == 2) {
            if (recode.getLevel() > 0) {
                recode.setLevel(recode.getLevel() - 1);
            }
            recode.setReview(LocalDate.now());
        }
        return recode;
    }


    //获取下次复习的时间
    public static LocalDate getReviewTime(LocalDate lastTime , int level) {
        return switch (level) {
            case 0 -> lastTime;
            case 1 -> lastTime.plusDays(1);
            case 2 -> lastTime.plusDays(2);
            case 3 -> lastTime.plusDays(4);
            case 4 -> lastTime.plusDays(8);
            case 5 -> lastTime.plusDays(14);
            case 6 -> lastTime.plusDays(22);
            case 7 -> lastTime.plusDays(32);
            case 8 -> lastTime.plusDays(44);
            default -> null; // 添加一个默认分支以处理其他情况
        };
    }

    // 获取该日需复习的单词
    public   List<CardChunks>  getReviewWords(String token) {
        int uid = jwtService.deconstruction(token).getUid();
//        List<Integer> cidS = new ArrayList<>();
        List<Recode> recodes =  recodeMapper.getRecodeForReview(uid, LocalDate.now());
//        for (Recode recode : recodes) {
//            if (recode.getReview().isBefore(LocalDate.now())) {
//                cidS.add(recode.getCid());
//            }
//        }
        return getReviewById(recodes, uid);
    }

    //获取该日需复习的单词(该产品）
    public   List<CardChunks>  getReview(int pid,String token){
        int uid = jwtService.deconstruction(token).getUid();
//        List<Integer> cidS = new ArrayList<>();
        List<Recode> recodes =  recodeMapper.getRecodeForReviewById(uid, LocalDate.now(), pid);
//        for (Recode recode : recodes) {
//            if (recode.getReview().isBefore(LocalDate.now()) || recode.getReview().isEqual(LocalDate.now())) {
//                cidS.add(recode.getCid());
//            }
//        }
        return getReviewById(recodes, uid);

    }

    public List<CardChunks> getReviewById( List<Recode> recodeList, int uid){
        List<CardChunks> reviewList = new ArrayList<>();
        for (Recode cid : recodeList) {
            CardChunks cardChunks = cardChunksMapper.getCardById(cid.getCid());
            if(cardChunks != null){
                cardChunks.setIsCollect(cardChunksMapper.isCollect(cid.getCid(), uid));
                reviewList.add(cardChunks);
            }

        }
        return reviewList;
    }

    //随便刷刷
    public List<CardChunks> getRandomList(String token, Integer pid, Integer num){
        return recodeMapper.getRandomList( pid, num.toString());
    }





}
